package com.situ.crm.rbac.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.crm.common.utils.JSONResult;
import com.situ.crm.rbac.pojo.User;
import com.situ.crm.rbac.service.IUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Zhou
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/rbac/user")
//解决跨域问题的一种方式
//@CrossOrigin
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * value:接口简称
     * notes:接口的详细描述
     */
    @ApiOperation(value = "登录", notes = "输入用户名和密码验证是否可以登录成功")
    //对于swagger。尽量不要使用 @RequestMapping 因为swagger会为接口生成7种请求方式的接口文档
    @PostMapping("/login")
    public JSONResult login() {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", "admin");
        return JSONResult.ok(map);
    }

    @ApiIgnore
    @GetMapping("/info")
    public JSONResult info() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "admin");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return JSONResult.ok(map);
    }

    @ApiIgnore
    @PostMapping("/logout")
    public JSONResult logout() {
        System.out.println("UserController.logout");
        return JSONResult.ok("退出成功");
    }

    @ApiOperation(value = "分页", notes = "根据第几页page和每页多少条查找指定页数据")
    @GetMapping("/selectByPage")
    public JSONResult selectByPage(@RequestParam(value = "page", required = false, defaultValue = "1") Integer current, @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        IPage<User> page = new Page<>(current, limit);
        userService.page(page, null);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return JSONResult.ok(map);
    }

    @ApiIgnore
    @GetMapping("/selectAll")
    public JSONResult selectAll() {
        List<User> list = userService.list();
        return JSONResult.ok(list);
    }
    @ApiOperation(value = "根据id查询", notes = "根据用户id查询数据")
    @GetMapping("/selectById/{id}")
    public JSONResult selectById(@PathVariable Integer id) {
        User user = userService.getById(id);
        return JSONResult.ok(user);
    }

    @ApiOperation(value = "删除", notes = "根据输入的id进行删除")
    @DeleteMapping("/deleteById/{id}")
    public JSONResult deleteById(@PathVariable Integer id) {
        boolean isSuccess = userService.removeById(id);
        return isSuccess ? JSONResult.ok("删除成功") : JSONResult.error("删除失败");
    }
    @ApiOperation(value = "添加", notes = "根据输入信息进行用户的添加")
    @PostMapping("/add")
    public JSONResult add(@RequestBody User user) {
        boolean save = userService.save(user);
        return save ? JSONResult.ok("添加成功") : JSONResult.error("添加失败");
    }
    @ApiOperation(value = "修改", notes = "根据输入信息进行用户的修改")
    @PutMapping("/update")
    public JSONResult update(@RequestBody User user) {
        boolean save = userService.updateById(user);
        return save ? JSONResult.ok("修改成功") : JSONResult.error("修改失败");
    }
}



